package org.light.simpleauth;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.complexverb.ListMyActive;
import org.light.core.LayoutComb;
import org.light.core.ReportComb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.ManyToMany;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.generator.Generator;
import org.light.utils.DomainUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class LoginFilterGenerator extends Generator{	
    protected Domain userDomain;
    protected Domain roleDomain;
    protected Domain privilegeDomain;
    protected List<Domain> domains;
	protected Set<ManyToMany> mtms;
	protected List<LayoutComb> layouts;
    protected List<ReportComb> reports;    

    public Set<ManyToMany> getMtms() {
        return mtms;
    }

    public void setMtms(Set<ManyToMany> mtms) {
        this.mtms = mtms;
    }

    public List<Domain> getDomains() {
        return domains;
    }

    public void setDomains(List<Domain> domains) {
        this.domains = domains;
    }
    
    public List<LayoutComb> getLayouts() {
        return layouts;
    }

    public void setLayouts(List<LayoutComb> layouts) {
        this.layouts = layouts;
    }

    public List<ReportComb> getReports() {
        return reports;
    }

    public void setReports(List<ReportComb> reports) {
        this.reports = reports;
    }

    public Domain getUserDomain() {
        return userDomain;
    }

    public void setUserDomain(Domain userDomain) {
        this.userDomain = userDomain;
    }

    public Domain getRoleDomain() {
        return roleDomain;
    }

    public void setRoleDomain(Domain roleDomain) {
        this.roleDomain = roleDomain;
    }

    public Domain getPrivilegeDomain() {
        return privilegeDomain;
    }

    public void setPrivilegeDomain(Domain privilegeDomain) {
        this.privilegeDomain = privilegeDomain;
    }

    public LoginFilterGenerator(){
		super();		
		this.standardName = "LoginFilter";
		this.fileName = "LoginFilter.java";
	}
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
    	List<Writeable> sList = new ArrayList<Writeable>();
        sList.add(new Statement(1000L,0,"package "+this.userDomain.getPackageToken()+".filter;"));
        sList.add(new Statement(2000L,0,""));
        sList.add(new Statement(3000L,0,"import java.io.IOException;"));
        sList.add(new Statement(4000L,0,"import java.util.Collections;"));
        sList.add(new Statement(5000L,0,"import java.util.Set;"));
        sList.add(new Statement(6000L,0,"import java.util.TreeSet;"));
        sList.add(new Statement(7000L,0,"import java.util.regex.Matcher;"));
        sList.add(new Statement(8000L,0,"import java.util.regex.Pattern;"));
        sList.add(new Statement(9000L,0,""));
        sList.add(new Statement(10000L,0,"import javax.servlet.Filter;"));
        sList.add(new Statement(11000L,0,"import javax.servlet.FilterChain;"));
        sList.add(new Statement(12000L,0,"import javax.servlet.FilterConfig;"));
        sList.add(new Statement(13000L,0,"import javax.servlet.ServletException;"));
        sList.add(new Statement(14000L,0,"import javax.servlet.ServletRequest;"));
        sList.add(new Statement(15000L,0,"import javax.servlet.ServletResponse;"));
        sList.add(new Statement(16000L,0,"import javax.servlet.annotation.WebFilter;"));
        sList.add(new Statement(17000L,0,"import javax.servlet.http.HttpServletRequest;"));
        sList.add(new Statement(18000L,0,"import javax.servlet.http.HttpServletResponse;"));
        sList.add(new Statement(19000L,0,""));
        sList.add(new Statement(20000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getDomainSuffix()+"."+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+";"));
        sList.add(new Statement(21000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getDomainSuffix()+"."+this.roleDomain.getCapFirstDomainNameWithSuffix()+";"));
        sList.add(new Statement(22000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getDomainSuffix()+"."+this.userDomain.getCapFirstDomainNameWithSuffix()+";"));
        sList.add(new Statement(23000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getServiceSuffix()+"."+this.roleDomain.getCapFirstDomainName()+"Service;"));
        sList.add(new Statement(24000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getServiceimplSuffix()+"."+this.roleDomain.getCapFirstDomainName()+"ServiceImpl;"));
        sList.add(new Statement(25000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getServiceSuffix()+"."+this.userDomain.getCapFirstDomainName()+"Service;"));
        sList.add(new Statement(26000L,0,"import "+this.userDomain.getPackageToken()+"."+this.userDomain.getServiceimplSuffix()+"."+this.userDomain.getCapFirstDomainName()+"ServiceImpl;"));
        
        sList.add(new Statement(27000L,0,"import "+this.userDomain.getPackageToken()+".utils.AppTokenManager;"));
        sList.add(new Statement(28000L,0,"import "+this.userDomain.getPackageToken()+".utils.StringUtil;"));
        sList.add(new Statement(29000L,0,""));
        sList.add(new Statement(30000L,0,"@WebFilter(urlPatterns = {\"/pages/*\",\"/"+this.userDomain.getControllerSuffix()+"/*\"})"));
        sList.add(new Statement(31000L,0,"public class LoginFilter implements Filter {"));
        sList.add(new Statement(32000L,1,"public void init(FilterConfig filterConfig) throws ServletException {"));
        sList.add(new Statement(33000L,1,"}"));
        sList.add(new Statement(34000L,0,""));
        sList.add(new Statement(35000L,1,"public void doFilter(ServletRequest request, ServletResponse response,"));
        sList.add(new Statement(36000L,3,"FilterChain chain) throws IOException, ServletException {"));
        sList.add(new Statement(37000L,2,"try {"));
        sList.add(new Statement(38000L,3,"HttpServletRequest req = (HttpServletRequest) request;"));
        sList.add(new Statement(39000L,3,"HttpServletResponse res = (HttpServletResponse) response;"));
        sList.add(new Statement(40000L,0,""));
        sList.add(new Statement(41000L,3,this.userDomain.getCapFirstDomainNameWithSuffix()+" "+this.userDomain.getLowerFirstDomainName()+" = ("+this.userDomain.getCapFirstDomainNameWithSuffix()+") req.getSession().getAttribute(\"user\");"));
        sList.add(new Statement(42000L,3,"String "+this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = \"\";"));
        sList.add(new Statement(43000L,3,"if ("+this.userDomain.getLowerFirstDomainName()+" != null) "+this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = "+this.userDomain.getLowerFirstDomainName()+".get"+this.userDomain.findFieldByFixedName("userName").getCapFirstFieldName()+"();"));
        sList.add(new Statement(44000L,3,"String token = req.getHeader(\"X-Token\");"));
        sList.add(new Statement(45000L,3,"if (!StringUtil.isBlank(token)){"));
        sList.add(new Statement(46000L,4,this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = AppTokenManager.getUserNameViaToken(token);"));
        sList.add(new Statement(47000L,3,"}"));
        sList.add(new Statement(48000L,0,""));
        sList.add(new Statement(49000L,3,"if (StringUtil.isBlank("+this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+")){"));
        sList.add(new Statement(50000L,4,"req.getRequestDispatcher(\"/login/index.html\").forward(req, res);"));
        sList.add(new Statement(51000L,4,"return;"));
        sList.add(new Statement(52000L,3,"}"));
        sList.add(new Statement(53000L,0,""));
        sList.add(new Statement(54000L,3,"String forwardUrl = req.getRequestURI();"));
        sList.add(new Statement(55000L,3,"String[] forwards = forwardUrl.split(\"/\");"));
        sList.add(new Statement(56000L,3,"String forwardUrl0 = \"\";"));
        sList.add(new Statement(57000L,3,"if (forwards.length >= 3) {"));
        sList.add(new Statement(58000L,4,"forwardUrl0 = \"/\" + forwards[forwards.length - 2] + \"/\""));
        sList.add(new Statement(59000L,6,"+ forwards[forwards.length - 1];"));
        sList.add(new Statement(60000L,3,"}"));
        sList.add(new Statement(61000L,0,""));
        sList.add(new Statement(62000L,3,"boolean canAccess = canAccess("+this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+", forwardUrl0);"));
        sList.add(new Statement(63000L,3,"if (!canAccess){"));
        sList.add(new Statement(64000L,4,"req.getRequestDispatcher(\"/login/index.html\").forward(req, res);"));
        sList.add(new Statement(65000L,4,"return;"));
        sList.add(new Statement(66000L,3,"}"));
        sList.add(new Statement(67000L,3,"chain.doFilter(request, response);"));
        sList.add(new Statement(68000L,2,"} catch (Exception e){"));
        sList.add(new Statement(69000L,3,"e.printStackTrace();"));
        sList.add(new Statement(70000L,3,"HttpServletResponse res = (HttpServletResponse) response;"));
        sList.add(new Statement(71000L,3,"HttpServletRequest req = (HttpServletRequest) request;"));
        sList.add(new Statement(72000L,3,"req.getRequestDispatcher(\"/login/index.jsp\").forward(req, res);"));
        sList.add(new Statement(73000L,2,"} finally {"));
        sList.add(new Statement(74000L,2,"}"));
        sList.add(new Statement(75000L,1,"}"));
        sList.add(new Statement(76000L,0,""));
        sList.add(new Statement(77000L,1,"public void destroy() {"));
        sList.add(new Statement(78000L,1,"}"));
        sList.add(new Statement(79000L,0,""));
        sList.add(new Statement(80000L,1,"protected boolean canAccess(String "+this.userDomain.getLowerFirstDomainName()+"Name, String path) throws Exception {"));
        sList.add(new Statement(81000L,1,this.userDomain.getCapFirstDomainName()+"Service service = new "+this.userDomain.getCapFirstDomainName()+"ServiceImpl();"));
		sList.add(new Statement(82000L,1,this.roleDomain.getCapFirstDomainName()+"Service "+this.roleDomain.getLowerFirstDomainName()+"Service = new "+this.roleDomain.getCapFirstDomainName()+"ServiceImpl();"));
        FindByName find = new FindByName(this.userDomain);
		sList.add(new Statement(83000L,1,this.userDomain.getCapFirstDomainNameWithSuffix()+" "+this.userDomain.getLowerFirstDomainName()+" = service."+StringUtil.lowerFirst(find.getVerbName())+"("+this.userDomain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
        ListMyActive listRoles = new ListMyActive(this.userDomain,this.roleDomain);
		sList.add(new Statement(84000L,1,"Set<"+this.roleDomain.getCapFirstDomainNameWithSuffix()+"> "+this.roleDomain.getLowerFirstPlural()+" = service."+StringUtil.lowerFirst(listRoles.getVerbName())+"("+this.userDomain.getLowerFirstDomainName()+".get"+this.userDomain.getDomainId().getCapFirstFieldName()+"());"));

        sList.add(new Statement(85000L,2,"Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> "+this.privilegeDomain.getLowerFirstPlural()+" = new TreeSet<>();"));
        sList.add(new Statement(86000L,2,"for ("+this.roleDomain.getCapFirstDomainNameWithSuffix()+" "+this.roleDomain.getLowerFirstDomainName()+" : "+this.roleDomain.getLowerFirstPlural()+") {"));
        ListMyActive listPrivs = new ListMyActive(this.roleDomain,this.privilegeDomain);
        sList.add(new Statement(87000L,3,"Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> subPrivs = "+this.roleDomain.getLowerFirstDomainName()+"Service."+StringUtil.lowerFirst(listPrivs.getVerbName())+"("+this.roleDomain.getLowerFirstDomainName()+".get"+this.roleDomain.getDomainId().getCapFirstFieldName()+"());"));
        sList.add(new Statement(88000L,3,this.privilegeDomain.getLowerFirstPlural()+".addAll(subPrivs);"));
        sList.add(new Statement(89000L,2,"}"));        
        sList.add(new Statement(90000L,2,"if (containsAdminRole("+this.roleDomain.getLowerFirstPlural()+")) {"));
        sList.add(new Statement(91000L,3,"return true;"));
        sList.add(new Statement(92000L,2,"}else{"));
        sList.add(new Statement(93000L,3,"return containsAllPrivileges("+this.privilegeDomain.getLowerFirstPlural()+", path);"));
        sList.add(new Statement(94000L,2,"}"));
        sList.add(new Statement(95000L,1,"}"));
        sList.add(new Statement(96000L,0,""));
        sList.add(new Statement(97000L,1,"protected boolean containsAdminRole(Set<"+this.roleDomain.getCapFirstDomainNameWithSuffix()+"> roles) {"));
        sList.add(new Statement(98000L,2,"for ("+this.roleDomain.getCapFirstDomainNameWithSuffix()+" role : roles){"));
        sList.add(new Statement(99000L,3,"if (\"admin\".equals(role.get"+this.roleDomain.getDomainName().getCapFirstFieldName()+"())){"));
        sList.add(new Statement(100000L,4,"return true;"));
        sList.add(new Statement(101000L,3,"}"));
        sList.add(new Statement(102000L,2,"}"));
        sList.add(new Statement(103000L,2,"return false;"));
        sList.add(new Statement(104000L,1,"}"));
        sList.add(new Statement(105000L,0,""));
        sList.add(new Statement(106000L,1,"protected boolean containsPrivilege(Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> privileges,String privilegeName) {"));
        sList.add(new Statement(107000L,2,"boolean contains = false;"));
        sList.add(new Statement(108000L,2,"for ("+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+" privilege : privileges) {"));
        sList.add(new Statement(109000L,3,"if (privilege.get"+this.privilegeDomain.getDomainName().getCapFirstFieldName()+"().equals(privilegeName)) {"));
        sList.add(new Statement(110000L,4,"contains = true;"));
        sList.add(new Statement(111000L,3,"}"));
        sList.add(new Statement(112000L,2,"}"));
        sList.add(new Statement(113000L,2,"return contains;"));
        sList.add(new Statement(114000L,1,"}"));
        sList.add(new Statement(115000L,0,""));
        sList.add(new Statement(116000L,1,"protected boolean checkLogin(Set<String> requirePrivs) {"));
        sList.add(new Statement(117000L,2,"for (String priv : requirePrivs){"));
        sList.add(new Statement(118000L,3,"if (\"annon\".equals(priv) || \"authc\".equals(priv)){"));
        sList.add(new Statement(119000L,4,"return true;"));
        sList.add(new Statement(120000L,3,"}"));
        sList.add(new Statement(121000L,2,"}"));
        sList.add(new Statement(122000L,2,"return false;"));
        sList.add(new Statement(123000L,1,"}"));
        sList.add(new Statement(124000L,0,""));
        sList.add(new Statement(125000L,1,"protected boolean containsStrPriv(Set<String> requirePrivs, String strPriv) {"));
        sList.add(new Statement(126000L,2,"for (String priv : requirePrivs){"));
        sList.add(new Statement(127000L,3,"if (strPriv.equals(priv)){"));
        sList.add(new Statement(128000L,4,"return true;"));
        sList.add(new Statement(129000L,3,"}"));
        sList.add(new Statement(130000L,2,"}"));
        sList.add(new Statement(131000L,2,"return false;"));
        sList.add(new Statement(132000L,1,"}"));
        sList.add(new Statement(133000L,0,""));
        sList.add(new Statement(134000L,1,"protected boolean containsAllPrivileges(Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> privs, String uri) {"));
        sList.add(new Statement(135000L,2,"Set<String> requirePrivs = getPermsByPath(uri);"));
        sList.add(new Statement(136000L,2,"if (checkLogin(requirePrivs)) {"));
        sList.add(new Statement(137000L,3,"return true;"));
        sList.add(new Statement(138000L,2,"} else if (containsStrPriv(requirePrivs,\"noauth\")) {"));
        sList.add(new Statement(139000L,3,"return false;"));
        sList.add(new Statement(140000L,2,"} else  if (privs.size() == 0) {"));
        sList.add(new Statement(141000L,3,"return false;"));
        sList.add(new Statement(142000L,2,"} else {"));
        sList.add(new Statement(143000L,3,"for (String privilege : requirePrivs) {"));
        sList.add(new Statement(144000L,4,"if (!containsPrivilege(privs,privilege)) {"));
        sList.add(new Statement(145000L,5,"return false;"));
        sList.add(new Statement(146000L,4,"}"));
        sList.add(new Statement(147000L,3,"}"));
        sList.add(new Statement(148000L,3,"return true;"));
        sList.add(new Statement(149000L,2,"}"));
        sList.add(new Statement(150000L,1,"}"));
        sList.add(new Statement(151000L,0,""));
        sList.add(new Statement(152000L,1,"protected Set<String> getPermsByPath(String path){"));
        sList.add(new Statement(153000L,2,"if (path.equals(\"/\")) {"));
        sList.add(new Statement(154000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(155000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(156000L,3,"return  perms;"));
        sList.add(new Statement(157000L,2,"}"));
        sList.add(new Statement(158000L,2,"if (path.equals(\"/favicon.ico\")) {"));
        sList.add(new Statement(159000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(160000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(161000L,3,"return  perms;"));
        sList.add(new Statement(162000L,2,"}"));
        sList.add(new Statement(163000L,0,""));
        sList.add(new Statement(164000L,2,"if (path.equals(\"/index.html\")) {"));
        sList.add(new Statement(165000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(166000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(167000L,3,"return  perms;"));
        sList.add(new Statement(168000L,2,"}"));
        sList.add(new Statement(169000L,0,""));
        sList.add(new Statement(170000L,2,"if (path.contains(\"/css/\")) {"));
        sList.add(new Statement(171000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(172000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(173000L,3,"return  perms;"));
        sList.add(new Statement(174000L,2,"}"));
        sList.add(new Statement(175000L,0,""));
        sList.add(new Statement(176000L,2,"if (path.contains(\"/images/\")) {"));
        sList.add(new Statement(177000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(178000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(179000L,3,"return  perms;"));
        sList.add(new Statement(180000L,2,"}"));
        sList.add(new Statement(181000L,0,""));
        sList.add(new Statement(182000L,2,"if (path.contains(\"/js/\")) {"));
        sList.add(new Statement(183000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(184000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(185000L,3,"return  perms;"));
        sList.add(new Statement(186000L,2,"}"));
        sList.add(new Statement(187000L,0,""));
        sList.add(new Statement(188000L,2,"if (path.contains(\"/easyui/\")) {"));
        sList.add(new Statement(189000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(190000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(191000L,3,"return  perms;"));
        sList.add(new Statement(192000L,2,"}"));
        sList.add(new Statement(193000L,0,""));
        sList.add(new Statement(194000L,2,"if (path.contains(\"/echarts/\")) {"));
        sList.add(new Statement(195000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(196000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(197000L,3,"return  perms;"));
        sList.add(new Statement(198000L,2,"}"));
        sList.add(new Statement(199000L,0,""));
        sList.add(new Statement(200000L,2,"if (path.contains(\"/uploadjs/\")) {"));
        sList.add(new Statement(201000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(202000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(203000L,3,"return  perms;"));
        sList.add(new Statement(204000L,2,"}"));
        sList.add(new Statement(205000L,0,""));
        sList.add(new Statement(206000L,2,"if (path.equals(\"/login/index.html\")) {"));
        sList.add(new Statement(207000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(208000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(209000L,3,"return  perms;"));
        sList.add(new Statement(210000L,2,"}"));
        sList.add(new Statement(211000L,0,""));
        sList.add(new Statement(212000L,2,"if (path.equals(\"/login/register.html\")) {"));
        sList.add(new Statement(213000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(214000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(215000L,3,"return  perms;"));
        sList.add(new Statement(216000L,2,"}"));
        sList.add(new Statement(217000L,0,""));
        sList.add(new Statement(218000L,2,"if (path.equals(\"/login/error.html\")) {"));
        sList.add(new Statement(219000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(220000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(221000L,3,"return  perms;"));
        sList.add(new Statement(222000L,2,"}"));
        sList.add(new Statement(223000L,0,""));
        sList.add(new Statement(224000L,2,"if (path.equals(\"/login/noauth.html\")) {"));
        sList.add(new Statement(225000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(226000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(227000L,3,"return  perms;"));
        sList.add(new Statement(228000L,2,"}"));
        sList.add(new Statement(229000L,0,""));
        sList.add(new Statement(230000L,2,"if (path.equals(\"/pages/index.html\")) {"));
        sList.add(new Statement(231000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(232000L,3,"Collections.addAll(perms, \"authc\");"));
        sList.add(new Statement(233000L,3,"return  perms;"));
        sList.add(new Statement(234000L,2,"}"));
        sList.add(new Statement(235000L,0,""));
        sList.add(new Statement(236000L,2,"if (path.startsWith(\"/login"+this.userDomain.getControllerNamingSuffix()+"/\")) {"));
        sList.add(new Statement(237000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(238000L,3,"Collections.addAll(perms, \"annon\");"));
        sList.add(new Statement(239000L,3,"return  perms;"));
        sList.add(new Statement(240000L,2,"}"));
        sList.add(new Statement(241000L,0,""));
        sList.add(new Statement(242000L,2,"if (path.startsWith(\"/profile"+this.userDomain.getControllerNamingSuffix()+"/\")) {"));
        sList.add(new Statement(243000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(244000L,3,"Collections.addAll(perms, \"authc\");"));
        sList.add(new Statement(245000L,3,"return  perms;"));
        sList.add(new Statement(246000L,2,"}"));
        sList.add(new Statement(247000L,0,""));
        sList.add(new Statement(248000L,2,"String re = \"/*"+this.userDomain.getControllerNamingSuffix()+"/find*\";"));
        sList.add(new Statement(249000L,2,"Pattern pattern = Pattern.compile(re);"));
        sList.add(new Statement(250000L,2,"Matcher matcher = pattern.matcher(path);"));
        sList.add(new Statement(251000L,2,"if (matcher.find()){"));
        sList.add(new Statement(252000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(253000L,3,"Collections.addAll(perms, \"authc\");"));
        sList.add(new Statement(254000L,3,"return  perms;"));
        sList.add(new Statement(255000L,2,"}"));
        sList.add(new Statement(256000L,0,""));
        sList.add(new Statement(257000L,2,"String re2 = \"/*"+this.userDomain.getControllerNamingSuffix()+"/listActive*\";"));
        sList.add(new Statement(258000L,2,"Pattern pattern2 = Pattern.compile(re2);"));
        sList.add(new Statement(259000L,2,"Matcher matcher2 = pattern2.matcher(path);"));
        sList.add(new Statement(260000L,2,"if (matcher2.find()){"));
        sList.add(new Statement(261000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(262000L,3,"Collections.addAll(perms, \"authc\");"));
        sList.add(new Statement(263000L,3,"return  perms;"));
        sList.add(new Statement(264000L,2,"}"));
        sList.add(new Statement(265000L,0,""));
        sList.add(new Statement(266000L,2,"String re3 = \"/*"+this.userDomain.getControllerNamingSuffix()+"/listAll*\";"));
        sList.add(new Statement(267000L,2,"Pattern pattern3 = Pattern.compile(re3);"));
        sList.add(new Statement(268000L,2,"Matcher matcher3 = pattern3.matcher(path);"));
        sList.add(new Statement(269000L,2,"if (matcher3.find()){"));
        sList.add(new Statement(270000L,3,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(271000L,3,"Collections.addAll(perms, \"authc\");"));
        sList.add(new Statement(272000L,3,"return  perms;"));
        sList.add(new Statement(273000L,2,"}"));
        sList.add(new Statement(274000L,0,""));

		long serial = 275000L;
		Set<Domain> adminDomains = new TreeSet<>();
		adminDomains.add(this.getUserDomain());
		adminDomains.add(this.getRoleDomain());
		adminDomains.add(this.getPrivilegeDomain());
		if (this.getDomains()!=null&&this.getDomains().size()>0) {
			Set<Domain> myDomains = new TreeSet<>();
			myDomains.addAll(this.getDomains());
			for (Domain d:myDomains) {
				if (!DomainUtil.inDomainSet(d,adminDomains)) {
                    sList.add(new Statement(serial,2,"if (path.equals(\"/pages/"+d.getPlural().toLowerCase()+".html\")) {"));
                    sList.add(new Statement(serial+1000L,2,"}"));
                    sList.add(new Statement(serial+2000L,0,""));
                    sList.add(new Statement(serial+3000L,2,"if (path.startsWith(\"/"+d.getLowerFirstDomainName()+d.getControllerNamingSuffix()+"/\")) {"));
                    sList.add(new Statement(serial+4000L+1000L,3,"Set<String> perms = new TreeSet<>();"));
                    sList.add(new Statement(serial+5000L,3,"Collections.addAll(perms, \""+d.getCapFirstDomainName()+"\");"));
                    sList.add(new Statement(serial+6000L,3,"return  perms;"));
                    sList.add(new Statement(serial+7000L,2,"}"));
                    sList.add(new Statement(serial+8000L,0,""));
                    serial += 9000L;
                }
            }
        }
        
        if (this.getMtms()!=null&&this.getMtms().size()>0) {
			for (ManyToMany mtm:this.getMtms()) {
				if (!DomainUtil.inDomainSet(mtm.getMaster(), adminDomains)) {
                    sList.add(new Statement(serial,2,"if (path.equals(\"/pages/"+mtm.getStandardName().toLowerCase()+".html\")) {"));
                    sList.add(new Statement(serial+1000L,3,"Set<String> perms = new TreeSet<>();"));
                    sList.add(new Statement(serial+2000L,3,"Collections.addAll(perms, \""+mtm.getMaster().getStandardName()+"\");"));
                    sList.add(new Statement(serial+3000L,3,"return  perms;"));
                    sList.add(new Statement(serial+4000L,2,"}"));
                    sList.add(new Statement(serial+5000L,0,""));
                    serial += 6000L;
                }
            }
        } 

        if (this.getLayouts()!=null&&this.getLayouts().size()>0) {
			for (LayoutComb lc:this.getLayouts()) {
				Set<Domain> mydomains = lc.getDomains();
				mydomains.removeAll(adminDomains);
                sList.add(new Statement(serial,2,"if (path.equals(\"/pages/"+lc.getStandardName().toLowerCase()+".html\")) {"));
                sList.add(new Statement(serial+1000L,3,"Set<String> perms = new TreeSet<>();"));
                sList.add(new Statement(serial+2000L,3,"Collections.addAll(perms, "+DomainUtil.DomainSetNamesWithQuote(mydomains)+");"));
                sList.add(new Statement(serial+3000L,3,"return  perms;"));
                sList.add(new Statement(serial+4000L,2,"}"));
				serial += 5000L;
			}
		}

		if (this.getReports()!=null&&this.getReports().size()>0) {
			for (ReportComb rc:this.getReports()) {
				Set<Domain> mydomains = rc.getDomains();
				mydomains.removeAll(adminDomains);
                sList.add(new Statement(serial,2,"if (path.equals(\"/pages/"+rc.getStandardName().toLowerCase()+".html\")) {"));
                sList.add(new Statement(serial+1000L,3,"Set<String> perms = new TreeSet<>();"));
                sList.add(new Statement(serial+2000L,3,"Collections.addAll(perms, "+DomainUtil.DomainSetNamesWithQuote(mydomains)+");"));
                sList.add(new Statement(serial+3000L,3,"return  perms;"));
                sList.add(new Statement(serial+4000L,2,"}"));
                sList.add(new Statement(serial+5000L,0,""));
				serial += 6000L;
			}
		}

        sList.add(new Statement(545000L,2,"Set<String> perms = new TreeSet<>();"));
        sList.add(new Statement(546000L,2,"Collections.addAll(perms, \"noauth\");"));
        sList.add(new Statement(547000L,2,"return  perms;"));
        sList.add(new Statement(548000L,1,"}"));
        sList.add(new Statement(549000L,0,""));
        sList.add(new Statement(550000L,0,"}"));             

    	return WriteableUtil.merge(sList);
	}
}
